package com.example.shopsystem.control;

import com.example.shopsystem.aop.GetIdUtil;
import com.example.shopsystem.aop.Jurisdiction;
import com.example.shopsystem.entity.Administrator;
import com.example.shopsystem.entity.Business;
import com.example.shopsystem.entity.Good;
import com.example.shopsystem.entity.dto.QueryReturnDto;
import com.example.shopsystem.entity.vo.AmdPowVo;
import com.example.shopsystem.entity.dto.QueryTimeDto;
import com.example.shopsystem.entity.vo.OrderDetailVo;
import com.example.shopsystem.service.AdminServiceImpl;
import com.example.shopsystem.service.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/filter/admin")
public class AdminControl {
    @Autowired
    AdminServiceImpl adminService;
    @Autowired
    OrderServiceImpl orderService;

    @Jurisdiction(Jurisdiction = "上架商品")
    @PostMapping("/addItem")
    public Result<Object> addItem(@RequestBody Good good) {//增加商品
        if (good.getName() == null) {
            return Result.fieldMiss(null);
        }
        if (!adminService.addItem(good)) {
            return Result.Fail(null);
        }
        return Result.ok(null);

    }

    @Jurisdiction(Jurisdiction = "修改商品")
    @PostMapping("/modifyItem")
    public Result<Object> modifyItem(@RequestBody Good good) {//修改商品
        if (String.valueOf(good.getId()) == null) {
            return Result.fieldMiss(null);
        }
        if (!adminService.modifyItem(good)) {
            return Result.Fail(null);
        }
        return Result.ok(null);
    }

    @Jurisdiction(Jurisdiction = "修改管理员权限")
    @PostMapping("/modifyAdminPower")
    public Result<Object> modifyAdminPower(@RequestBody AmdPowVo amdPowVo) {//修改管理员权限
        if (amdPowVo.getAd_id() == null) {
            return Result.parameterFail(null);
        }
        if (!adminService.modifyAdminPower(amdPowVo)) {
            return Result.Fail(null);
        }

        return Result.ok(null);

    }

    @Jurisdiction(Jurisdiction = "查询管理员")
    @PostMapping("/queryAdminPow")
    public Result<Object> queryAdminPow(@RequestBody Administrator administrator) {//查询管理员不需要id

        if (administrator.getName() == null && administrator.getId() == null) {
            return Result.parameterFail(null);
        }
        if (administrator.getId().equals(0)) {
            return Result.ok(adminService.queryAdminName(administrator));
        }
        return Result.ok(adminService.queryAdmin(administrator));
    }

    @Jurisdiction(Jurisdiction = "查询管理员")
    @PostMapping("/queryAdminList")
    public Result<Object> queryAdminList() {

        return Result.ok(adminService.queryAdminList());

    }

    @Jurisdiction(Jurisdiction = "修改管理员权限")
    @PostMapping("/modifyAdmin")
    public Result<Object> modifyAdmin(@RequestBody Administrator administrator) {
        administrator.setId(GetIdUtil.getCurrentUser());
        if (administrator.getPhone() == null) {
            return Result.formatError(null);
        }
        if (!adminService.modifyAdmin(administrator)) {
            return Result.updateFail(null);
        }
        return Result.ok(null);
    }

    @Jurisdiction(Jurisdiction = "查看订单")
    @PostMapping("/ViewSales")
    public Result<Object> ViewSales(@RequestBody QueryTimeDto queryTimeDto) {//查询某个时间段的订单
        if (queryTimeDto.getBeginTime() == null) {
            return Result.queryFail(null);
        }
        return Result.ok(orderService.ViewSales(queryTimeDto.getBeginTime(), queryTimeDto.getDieTime()));
    }

    @Jurisdiction(Jurisdiction = "統計進貨表")
    @PostMapping("/PrintPurchaseOrder")
    public Result<Object> PrintPurchaseOrder(@RequestBody QueryTimeDto queryTimeDto) {//打印进货单
        if (queryTimeDto.getBeginTime() == null) {
            return Result.queryFail(null);
        }
        return Result.ok(adminService.goodList(queryTimeDto.getBeginTime(), queryTimeDto.getDieTime()));
    }

    @Jurisdiction(Jurisdiction = "查看订单")
    @PostMapping("/viewSalesPerson")
    public Result<Object> viewSalesPerson(@RequestBody Map map) {//查看订单
        System.out.println(map.get("orderId"));
        if (map.get("orderId") == null) {
            return Result.queryFail(null);
        }
        return Result.ok(adminService.ViewSales(Integer.valueOf((Integer) map.get("orderId"))));
    }

    @Jurisdiction(Jurisdiction = "查看退货单")
    @PostMapping("/returnGoodsList")
    public Result<Object> returnGoodsList(@RequestBody QueryReturnDto queryReturnDto) {//查看退货单
        if (queryReturnDto.getBeginTime() == null || String.valueOf(queryReturnDto.getState()) == null) {
            return Result.fieldMiss(null);
        }
        return Result.ok(adminService.returnGoodsList(queryReturnDto));
    }

    @Jurisdiction(Jurisdiction = "查看商家")
    @PostMapping("/viewBus")
    public Result<Object> viewBus(@RequestBody Map map) {//查看商家
        if (map.get("state") == null) {
            return Result.fieldMiss(null);
        }
        return Result.ok(adminService.viewBus(Integer.valueOf((Integer) map.get("state"))));
    }

    @Jurisdiction(Jurisdiction = "修改商家")
    @PostMapping("/updateBus")
    public Result<Object> updateBus(@RequestBody Business business) {
        if (business.getId() == null) {
            return Result.formatError(null);
        }
        if (!adminService.updateBus(business)) {
            return Result.formatError(null);
        }
        return Result.ok(null);
    }
}
